package com.ruoyi.caigou.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.ruoyi.basedata.api.RemoteGoodsService;
import com.ruoyi.caigou.domain.pojo.BuySuppliesPriceGoods;
import com.ruoyi.common.core.constant.SecurityConstants;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.exception.ServiceException;
import com.ruoyi.system.api.domain.basedata.Goods;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.caigou.mapper.BuySuppliesPriceMapper;
import com.ruoyi.caigou.domain.BuySuppliesPrice;
import com.ruoyi.caigou.service.IBuySuppliesPriceService;

/**
 * 采购供应商报价Service业务层处理
 *
 * @author lvql
 * @date 2024-10-31
 */
@Service
public class BuySuppliesPriceServiceImpl implements IBuySuppliesPriceService
{
    @Autowired
    private BuySuppliesPriceMapper buySuppliesPriceMapper;

    @Autowired
    private RemoteGoodsService remoteGoodsService;

    /**
     * 查询采购供应商报价
     *
     * @param id 采购供应商报价主键
     * @return 采购供应商报价
     */
    @Override
    public BuySuppliesPrice selectBuySuppliesPriceById(Integer id)
    {
        return buySuppliesPriceMapper.selectBuySuppliesPriceById(id);
    }

    /**
     * 查询采购供应商报价列表
     *
     * @param buySuppliesPrice 采购供应商报价
     * @return 采购供应商报价
     */
    @Override
    public List<BuySuppliesPrice> selectBuySuppliesPriceList(BuySuppliesPrice buySuppliesPrice)
    {
        return buySuppliesPriceMapper.selectBuySuppliesPriceList(buySuppliesPrice);
    }

    @Override
    public List<BuySuppliesPriceGoods> selectBuySuppliesPriceListByCodeList(List<String> codeList){

        List<BuySuppliesPriceGoods> buySuppliesPrices = new ArrayList<>();
        for (String s : codeList) {
            buySuppliesPrices.addAll(buySuppliesPriceMapper.selectBuySuppliesPriceByBuySupCode(s));
        }
        List<Integer> goodsIds = new ArrayList<>();
        for (BuySuppliesPrice buySuppliesPrice : buySuppliesPrices) {
            Integer goodsId = buySuppliesPrice.getGoodsId();
            goodsIds.add(goodsId);
        }
        R<List<Goods>> goodsListByIds = R.ok();
        if (!goodsIds.isEmpty()) {
            goodsListByIds =   remoteGoodsService.getGoodsListByIds(goodsIds, SecurityConstants.INNER);
        }
        if (R.FAIL== goodsListByIds.getCode()){
            throw new ServiceException("远程调用商品信息接口异常");
        }
        List<Goods> data = goodsListByIds.getData();
        for (BuySuppliesPriceGoods buySuppliesPrice : buySuppliesPrices) {
            for (Goods goods : data) {
                if (buySuppliesPrice.getGoodsId().equals(goods.getId())) {
                    buySuppliesPrice.setProCode(goods.getProCode());
                    buySuppliesPrice.setProName(goods.getProName());
                    buySuppliesPrice.setUnitName(goods.getUnitName());
                }
            }
        }
        return buySuppliesPrices;
    }

    /**
     * 新增采购供应商报价
     *
     * @param buySuppliesPrice 采购供应商报价
     * @return 结果
     */
    @Override
    public int insertBuySuppliesPrice(BuySuppliesPrice buySuppliesPrice)
    {
        return buySuppliesPriceMapper.insertBuySuppliesPrice(buySuppliesPrice);
    }

    /**
     * 修改采购供应商报价
     *
     * @param buySuppliesPrice 采购供应商报价
     * @return 结果
     */
    @Override
    public int updateBuySuppliesPrice(BuySuppliesPrice buySuppliesPrice)
    {
        return buySuppliesPriceMapper.updateBuySuppliesPrice(buySuppliesPrice);
    }

    /**
     * 批量删除采购供应商报价
     *
     * @param ids 需要删除的采购供应商报价主键
     * @return 结果
     */
    @Override
    public int deleteBuySuppliesPriceByIds(Integer[] ids)
    {
        return buySuppliesPriceMapper.deleteBuySuppliesPriceByIds(ids);
    }

    /**
     * 删除采购供应商报价信息
     *
     * @param id 采购供应商报价主键
     * @return 结果
     */
    @Override
    public int deleteBuySuppliesPriceById(Integer id)
    {
        return buySuppliesPriceMapper.deleteBuySuppliesPriceById(id);
    }
}
